from django.core.paginator import Paginator
from django.db.models import Q

from apps.aiauth.models import Users
from utils import restful
from .serializers import UserCareerPlanSerializers, UserIceModelSerializers


def CareerPlanStatistics(request):
    organization_id = request.GET.get('organization_id')
    search_dict = {}
    if organization_id:
        search_dict['userinfo__organization_id'] = organization_id

    userList = Users.objects.filter(role=1) \
        .filter(~Q(userinfo__organization__organization_id__in=[10, 11, 19, 20]))
    userList = userList.filter(
        **search_dict
    )
    pagenow = request.GET.get('pagenow', 1)
    size = request.GET.get('pagesize', 10)
    page_obj = Paginator(userList, size)
    page_data = page_obj.get_page(pagenow)
    totalcount = page_obj.count
    data = UserCareerPlanSerializers(page_data.object_list, many=True).data

    return restful.result(message="成功", data=data, kwargs={'totalcount': totalcount})


def IceModelStatistics(request):
    organization_id = request.GET.get('organization_id')
    search_dict = {}
    if organization_id:
        search_dict['userinfo__organization_id'] = organization_id
    pagenow = request.GET.get('pagenow', 1)
    size = request.GET.get('pagesize', 10)
    userList = Users.objects.filter(role=1).filter(~Q(userinfo__organization__organization_id__in=[10, 11, 19, 20]))
    userList = userList.filter(
        **search_dict
    )
    obj = Paginator(userList, size)
    page_data = obj.get_page(pagenow)
    totalcount = obj.count
    data = UserIceModelSerializers(page_data.object_list, many=True).data

    return restful.result(data=data, message="成功", kwargs={'totalcount': totalcount})
